/*
 * @Author: NtyAaron
 * @Date: 2021-04-12 11:13:45
 * @LastEditors: NtyAaron
 * @LastEditTime: 2021-06-23 14:25:51
 * @Description: file content
 * @FilePath: \dwq\pages\check\checkBus\checkBus.js
 */
const app = getApp()
import { getStartBusList, getBusByStart } from '../../../api/checkBus/checkBus'
import { formatDate } from '../../../utils/util'
import { _t } from '../../../i18n/base'
import { getPosterList } from '../../../api/advert/advert'
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'

Page({
  data: {
    fromShow: false,
    toShow: false,
    dateShow: false,
    departureTime: formatDate(new Date()),
    startCity: '',
    endCity: '',
    startCityList: [],
    FromCityRouteList: [],
    language: '',
    lang: {},
    getAdvertObj: {
      categoryType: '',
      language: '',
      posterName: '',
      posterType: 8,
      showType: 1,
      trafficType: 4,
    },
    bannerList: [],
    messagePath: '/pages/overview/overview',
    messageImg: '/images/logo新.png',
  },

  //vant 弹窗组件（用于进入客服）
  vantDialog(e) {
    Dialog.confirm({
      title: this.data.lang['查看内容'],
      message: this.data.lang['跳转内容链接'],
      confirmButtonText: this.data.lang['确定'],
      cancelButtonText: this.data.lang['取消'],
      confirmButtonOpenType: 'contact',
      sendMessageTitle: e.title,
      sendMessagePath: `${this.data.messagePath}?${e.id},${e.type}`,
      sendMessageImg: this.data.messageImg,
      showMessageCard: true,
    })
      .then(async () => {
        setTimeout(() => {
          console.log(12)
        })
      })
      .catch(() => {})
  },
  // 点击出发城市
  fromCitySelectHandle() {
    this.setData({
      fromShow: true,
    })
    this.getFromCity()
  },
  // 获取出发城市
  async getFromCity() {
    let { result } = await getStartBusList(this.data.language)
    if (result) {
      this.setData({
        startCityList: result,
      })
      wx.hideLoading()
    }
  },
  // 获取出发城市的交通路线
  async getFromCityRoute(e) {
    let { result } = await getBusByStart(e)
    if (result) {
      this.setData({
        fromShow: false,
        FromCityRouteList: result,
        toShow: true,
        endCity: result[0].arrCtyChsname,
      })
    }
  },
  // 关闭起始城市列表弹窗
  fromShowCloseHandle() {
    this.setData({
      fromShow: false,
    })
  },
  // 关闭路线弹窗
  toShowCloseHandle() {
    this.setData({
      toShow: false,
    })
  },
  // 点击出发城市
  cityHandle(e) {
    let cityName = e.currentTarget.dataset.name
    this.setData({
      startCity: cityName,
    })
    this.getFromCityRoute(cityName)
  },
  // 点击列表跳转
  cityRouteHandle(e) {
    e.currentTarget.dataset.info.dteDteId = this.data.departureTime
    let info = JSON.stringify(e.currentTarget.dataset.info)
    this.setData({
      toShow: false,
    })
    wx.navigateTo({
      url: `../busResult/busResult?info=${info}`,
    })
  },
  // 点击日期弹窗
  selectDepartureDateHandle() {
    this.setData({
      dateShow: !this.data.dateShow,
    })
  },
  // 点击日期
  onConfirm(e) {
    this.setData({
      departureTime: e.detail.date,
      dateShow: false,
    })
  },
  // 路线规划
  toMapHandle() {
    wx.navigateTo({
      url: '/pages/interactionMap/interactionMap',
    })
  },
  // 获取广告
  async getPosterList() {
    this.setData({
      'getAdvertObj.language': this.data.language,
    })
    let { result } = await getPosterList(this.data.getAdvertObj)
    if (result) {
      result.map((item) => {
        item.bannerLink = `${app.globalData.host}/attach/showPic?attachId=${item.pic}`
      })
      this.setData({
        bannerList: result,
      })
    }
  },
  // 复制
  copyLinkHandle() {
    let urlObj = {
      zh: 'https://www.gobybus.hk/bus/zh/?currency=HKD',
      en: 'https://www.gobybus.hk/bus/en/?currency=HKD',
      tw: 'https://www.gobybus.hk/bus/zh/?currency=HKD',
    }
    let language = wx.getStorageSync('language')
    let url = ''
    url = urlObj[language]
    wx.setClipboardData({
      data: url,
      success: () => {
        wx.showToast({
          title: this.data.lang['已复制网站网址'],
          icon: 'none',
        })
      },
    })
  },
  // 点击广告
  adClickHandle(e) {
    // let vantObj = {
    //   type: detail.type,
    //   id: detail.posterId,
    //   language: this.data.language,
    // }
    // this.vantDialog(vantObj)

    let link = e.currentTarget.dataset.link
    wx.setClipboardData({
      data: link,
      success: () => {
        wx.showToast({
          title: this.data.lang['已复制网站网址'],
          icon: 'none',
        })
      },
    })
  },
  onLoad() {
    this.setData({
      lang: _t(),
      language: wx.getStorageSync('lang'),
    })
    this.getPosterList()
    wx.setNavigationBarTitle({
      title: this.data.lang['巴士查询'],
    })
  },
})
